/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/json"
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
	. "saas_sys_base/modules/wechat/service"
)

type WxSignApiCtrl struct {
	wxSignApiService WxSignApiService
}

func init() {
	vc := new(WxSignApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *WxSignApiCtrl) CtrlInfo() string {
	return "WechatCardApiCtrl"
}

func (vc *WxSignApiCtrl) RoutInit() {
	lego.APIPOST("/api/mod/wechat/sign/ticket/cardticket", vc.GetJsSign, true)
	lego.APIPOST("/api/mod/wechat/sign/ticket/getticket", nil, true)
}

func (vc *WxSignApiCtrl) GetJsSign(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	type Request struct {
		NonceStr  string `json:"noncestr"`
		Timestamp string `json:"timestamp"`
		Url       string `json:"noncestr"`
	}
	type Resp struct {
		//AppId string `json:"card_id,omitempty"`

		Signature string `json:"sginature"`
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		signature, err := vc.wxSignApiService.GetJsSign(*appwho, request.NonceStr, request.Timestamp, request.Url)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.Signature = signature
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WxSignApiCtrl) GetCardSign(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	type Request struct {
		Code      string `json:"code,omitempty"`
		OpenId    string `json:"open_id,omitempty"`
		CardId    string `json:"card_id"`
		NonceStr  string `json:"noncestr"`
		Timestamp string `json:"timestamp"`
	}
	type Resp struct {
		Signature string `json:"signature"`
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		signature, err := vc.wxSignApiService.GetCardSign(*appwho, request.OpenId, request.Code, request.Timestamp, request.CardId, request.NonceStr)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.Signature = signature
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}
